/*
 * Created by SharpDevelop.
 * User: fatman
 * Date: 10/27/2008
 * Time: 3:11 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Collections;

namespace CUALib
{
	/// <summary>
	/// Description of ConsoleControlCollection.
	/// </summary>
	
	public class ConsoleControlCollection : IList<ConsoleControl>
	{

		List<ConsoleControl> _daList;


		private ConsoleControl _owner = null;
		public ConsoleControlCollection(ConsoleControl owner)
		{
			_daList = new List<ConsoleControl>();
			_owner = owner;
		}

		public ConsoleControlCollection()
		{
			_daList = new List<ConsoleControl>();

		}


		public int IndexOf(ConsoleControl c)
		{
			return _daList.IndexOf(c);
		}

		#region IList<ConsoleControl> Members


		public void Insert(int index, ConsoleControl item)
		{

			if (item.Parent != null)
			{
				item.Parent.Controls.Remove(item);
			}
			if (_owner != null)
			{
				item.Parent = _owner;
				//_owner.ActiveControl = item;
				if (item.Name == String.Empty)
				{
					item.Name = item.GetType().Name + _daList.Count.ToString();
				}
			}
			_daList.Insert(index, item);
			_owner.ControlAdded(item, index);
		}

		public void RemoveAt(int index)
		{
			if(_daList.Count>index)
			{
				ConsoleControl cc=_daList[index];
				_daList.RemoveAt(index);
				_owner.ControlAdded(cc, index);
			}
			
		}

		public ConsoleControl this [int index]
		{
			get
			{
				return _daList [index];
			}
			set
			{
				_daList [index] = value;
			}
		}

		#endregion

		#region ICollection<ConsoleControl> Members

		public void Add(ConsoleControl item)
		{
			if (item.Parent != null)
			{
				item.Parent.Controls.Remove(item);
			}

			if (_owner != null)
			{
				item.Parent = _owner;
				//_owner.ActiveControl = item;
				if (item.Name == String.Empty)
				{
					item.Name = item.GetType().Name + _daList.Count.ToString();
				}

			}
			_daList.Add(item);
			_owner.ControlAdded(item, _daList.Count-1);
		}

		public void Clear()
		{
			_daList.Clear();
		}

		public bool Contains(ConsoleControl item)
		{
			return _daList.Contains(item);
		}

		public void CopyTo(ConsoleControl [] array, int arrayIndex)
		{
			_daList.CopyTo(array, arrayIndex);
		}

		public int Count
		{
			get { return _daList.Count; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		public bool Remove(ConsoleControl item)
		{
			
			if(_daList.Contains(item))
			{
				
				int index=this.IndexOf(item);
				bool ret=_daList.Remove(item);
				_owner.ControlAdded(item, index);
				return ret;
			}

			
			return false;
		}

		#endregion

		#region IEnumerable<ConsoleControl> Members

		public IEnumerator<ConsoleControl> GetEnumerator()
		{
			return _daList.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		IEnumerator IEnumerable.GetEnumerator()
		{
			return _daList.GetEnumerator();
		}

		#endregion
	}

	
}
